#!/usr/bin/env ruby -wKU
#encoding: UTF-8

require 'backend/model/TNovel'

module Model
  
  class TVolume
    
    attr_accessor :novel # reference to novel instance
    attr_accessor :volName
    attr_accessor :chapters # reference to list of chapters 
    attr_accessor :maxChpNo # max chapter no of this volume
    attr_reader :volNo
    
    def initialize (novel)
      @novel = novel
      @volNo = 0
      @volName = ""
      @maxChpNo = 0
      @chapters = []
    end

    def volNo=(volNo)
      if volNo > @novel.maxVolNo
      @novel.maxVolNo = volNo
      end
      @volNo = volNo
    end

    def addChapter(chapter)
      @chapters << chapter unless @chapters.include? chapter
    end
    
    # get chapter in current volume
    def getChapterByChpNo(chpNo)
      @chapters.find do |chp|
        chp.chpNo == chpNo
      end
    end
	
    # get chapter in current volume
    def getChapter(chpNo, chpName)
	    chpNameTmp = chpName.nil? ? "" : chpName
	    @chapters.find do |chp|
	      chp.chpNo == chpNo && chp.chpName == chpNameTmp
	    end
    end
    
    def volumeDirName
      @volNo.to_s + "_" + @volName
    end

    def storageDir
      @novel.storageDir + "/" + volumeDirName
    end
    
    def ==(o)
      o.class == self.class && o.state == state 
    end
    alias_method :eql?, :==
    
    def hash
      state.hash
    end
    
    def state
      @novel.state.concat([@volNo, @volName])
    end

    
  end

  if __FILE__ == $0
    n = TNovel.new
    n.author= "辰东"
    n.novelName="aaa"
    n.tiebaName="cd"
    v = TVolume.new(n)
    p v.storageDir
    v.volNo=2
    v.volName="合理咯"
    p v.storageDir
  end
  
end

